package org.agile4j.patterns.singleton;

import java.util.HashMap;
import java.util.Map;

class OneExtend01 {
	private static final String DEFALUT_PREKEY = "Cache";
	private static final int MAX_NUM = 3;
	private static int count = 1;
	private static Map<String, Object> map = new HashMap<String, Object>();

	private OneExtend01() {
		System.out.println("111");
	}

	private static class SingletonHoder {
		public static OneExtend01 getInstance() {
			String key = DEFALUT_PREKEY + count;
			OneExtend01 vo = (OneExtend01) map.get(key);
			if (vo == null) {
				vo = new OneExtend01();
				map.put(key, vo);
			}
			count++;
			if (count > MAX_NUM) {
				count = 1;
			}
			return vo;
		}
	}

	public static OneExtend01 getInstance() {
		return SingletonHoder.getInstance();
	}
}

public class Test {
	public static void main(String[] args) {
		System.out.println(OneExtend01.getInstance());
		System.out.println(OneExtend01.getInstance());
		System.out.println(OneExtend01.getInstance());
		System.out.println(OneExtend01.getInstance());
		System.out.println(OneExtend01.getInstance());
		System.out.println(OneExtend01.getInstance());
		System.out.println(OneExtend01.getInstance());
		System.out.println(OneExtend01.getInstance());
	}
}
